package com.note.dao;
import java.sql.*;
public class DerbyDB {
	    private final static String driver = "org.apache.derby.jdbc.EmbeddedDriver";
	    private final static String protocol = "jdbc:derby:"; 
	    public static String dbName ="note";
	    Connection connection;
	    Statement statement;
	    public DerbyDB() { 
	    	String dbPath=DerbyDB.class.getClassLoader()
	    			.getResource("")
	    			.getPath();
	    	
	    	if (dbPath.startsWith("/c:/")||dbPath.startsWith("/C:/")||dbPath.startsWith("/d:/")||dbPath.startsWith("/D:/")||dbPath.startsWith("/e:/")||
	    			dbPath.startsWith("/E:/")||dbPath.startsWith("/f:/")||dbPath.startsWith("/F:/")||dbPath.startsWith("/g:/")||dbPath.startsWith("/G:/")) {
	    		System.out.println("dbPath="+dbPath);
	    		dbPath=dbPath.substring(1);
			}
	    	
	    	dbName=dbPath+"db/"+dbName;
	    
	    	System.out.println("dbName="+dbName);
	        try {
	            Class.forName(driver).newInstance();
	            connection = DriverManager.getConnection(protocol + dbName + ";user=root;password=root;create=true");
	        	statement = connection.createStatement();
	        	//creatTable_Note(connection);
	        } catch (Exception e) { 
	            e.printStackTrace(); 
	        }
	    }
	    public int creatTable_Note() {
	    	try {
	    	Statement statement = connection.createStatement();
	    
				statement.execute("create table note(id INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 100, INCREMENT BY 1),"
				 		+ "notedate TIMESTAMP,stringdate varchar(30),feel varchar(20),body varchar(20),dbwork varchar(800),"
				 		+ "xinde varchar(800),daiban varchar(800),zhichujine varchar(20),zhichumiaoshu varchar(800))");
				return 1;
	    	} catch (SQLException e) {
				e.printStackTrace();
				return -1;
			}
			
		}
	    

	    public void getDataFromDerby() {
	        try { 
 	            ResultSet resultSet = statement.executeQuery("select * from note");
	            while (resultSet.next()) {
	                System.out.println(resultSet.getString(1));
	                System.out.println(resultSet.getString(2));
	            }
	            statement.close();
	            resultSet.close();
	        } catch (Exception e1) {
	            e1.printStackTrace();
	        }
	    }

	    public static void main(String[] args) {
	        DerbyDB derbyTest = new DerbyDB(); 
	        derbyTest.getDataFromDerby();
	    }
}
